package com.example.springconfig.demos.config;

import com.example.springconfig.demos.model.User;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.ApplicationScope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;

@Configuration
public class BeanConfig {
    @Bean
    public User user1(){
        return new User(1,"zhangsan");
    }

    // 声明作用的范围(声明了一个单例的作用域)
    // 声明了一个单例的对象
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    // @Scope("singleton")
    @Bean
    public User singleUser(){
        return new User();
    }

    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean
    public User prototypeUser(){
        return new User();
    }

    @RequestScope
    @Bean
    public User requestUser(){
        return new User();
    }

    @SessionScope
    @Bean
    public User sessionUser(){
        return new User();
    }

    @ApplicationScope
    @Bean("applicationUser")// 指定名字
    public User applicationUser(){
        return new User();
    }
}
